package com.base.cn.platform.os.service.user.integral;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.user.integral.CusUserIntegralRuleDao;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegralRule;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * 用户积分规则
 */
@Service
public class CusUserIntegralRuleBiz extends BaseBiz<CusUserIntegralRule,CusUserIntegralRuleDao> {

    /**
     * 通过动作KEY，获取积分规则
     * @param actionKey 动作KEY
     * @return 积分规则对象
     */
    public CusUserIntegralRule getUserIntegralRuleByKey(String actionKey){
        return this.findOne(" actionKey='"+actionKey.trim()+"'",null);
    }

    /**
     * 分页搜索
     * @param cusUserIntegralRule
     * @param page
     * @return
     */
    public PageInfo<CusUserIntegralRule> queryCusUserExpRulePage(CusUserIntegralRule cusUserIntegralRule, Pagination page){
        String whereSql =whereSql(cusUserIntegralRule);
        return this.findPage(whereSql,page,new ArrayList<>());
    }

    /**
     * 添加修改积分规则
     * @param cusUserIntegralRule
     * @return
     */
    public  CusUserIntegralRule saveUpdateCusUserIntegralRule(CusUserIntegralRule cusUserIntegralRule){
        if(DataUtil.idIsNotNull(cusUserIntegralRule.getId())){
            CusUserIntegralRule _userIntegralRule = this.findById(cusUserIntegralRule.getId());
            _userIntegralRule.setScore(cusUserIntegralRule.getScore());
            if(_userIntegralRule.getType()==3){
                return  null;
            }
            this.updateById(_userIntegralRule,null);
            return _userIntegralRule;
        }else {
            cusUserIntegralRule.setStatus(1);
            if(cusUserIntegralRule.getType()==3){
                cusUserIntegralRule.setScore(0L);
            }
            this.save(cusUserIntegralRule);
            return cusUserIntegralRule;
        }
    }

    /**
     * 修改积分规则的状态 1 正常  2 停用   3 删除
     * @param integralRuleId
     * @param status
     * @return
     */
    public CusUserIntegralRule updateUserIntegralRuleStatus(BigDecimal integralRuleId, Integer status){
        CusUserIntegralRule userIntegralRule = this.findById(integralRuleId);
        userIntegralRule.setStatus(status);
        userIntegralRule.setId(integralRuleId);
        this.updateById(userIntegralRule,null);
        return userIntegralRule;
    }


    /**
     * sql 语句
     * @param cusUserIntegralRule
     * @return
     */
    public String  whereSql(CusUserIntegralRule cusUserIntegralRule){
        StringBuilder where = new StringBuilder(" status!=3 ");
        if(ObjectUtils.isNotEmpty(cusUserIntegralRule)){
            if (ObjectUtils.isNotEmpty(cusUserIntegralRule.getType())){
                if (DataUtil.idIsNotNull(BigDecimal.valueOf(cusUserIntegralRule.getType()))){
                    where .append( " and type =").append(cusUserIntegralRule.getType());
                }
            }
            if (ObjectUtils.isNotEmpty(cusUserIntegralRule.getType())){
                if(DataUtil.idIsNotNull(BigDecimal.valueOf(cusUserIntegralRule.getActionType()))){
                    where .append( " and actionType =").append(cusUserIntegralRule.getActionType());
                }
            }
            if (ObjectUtils.isNotEmpty(cusUserIntegralRule.getType())){
                if(DataUtil.idIsNotNull(BigDecimal.valueOf(cusUserIntegralRule.getStatus()))){
                    where .append( " and status =").append(cusUserIntegralRule.getStatus());
                }
            }
            if (ObjectUtils.isNotEmpty(cusUserIntegralRule.getIntegralRuleIds())){
                where .append( " and id in (").append(cusUserIntegralRule.getIntegralRuleIds()).append(")");
            }
            if (StringUtils.isNotEmpty(cusUserIntegralRule.getActionKeys())){
                String keyWords = "'"+cusUserIntegralRule.getActionKeys().replaceAll(",","','")+"'";
                where .append(" actionKey in (").append(keyWords).append(")");
            }
        }
        return where.toString();
    }
}
